import commons from "./common";

export function getBtn(isJump){

  let href = window.location.href;
  var data = window.localStorage.getItem('water_limit');
  var user =  window.localStorage.getItem('water_user');
  var obj = JSON.parse(data);
  if(isJump) {
    let name = href.split('name=')[1]
    let reName =  decodeURI(name)
    let treeData = JSON.parse(localStorage.getItem('tree'));
    for(let i = 0;i < treeData.length;i++) {
      for(let j = 0;j<treeData[i].childPermissions.length;j++) {
        if(treeData[i].childPermissions[j].name == reName) {
          var index = treeData[i].childPermissions[j].id;
          if (user) {
            if (data){
              let btn = obj[index];
              if (btn&&btn.query){
                return btn;
              }else {
                location.href = '/ErrorPage';
              }
            }else {
              location.href = '/ErrorPage';
            }
          }else {
            location.href = '/';
          }
        }
      }
    }
  }
  //非跳转进入
  else {
    let query = href.substring(href.indexOf('?')+1).split("&")[0];
    let index = query?query.substring(query.indexOf('=')+1):0;
    if (user) {
      if (data){
        let obj = JSON.parse(data);
        let btn = obj[index];
        if (btn&&btn.query){
          return btn;
        }else {
          location.href = '/ErrorPage';
        }
      }else {
        location.href = '/ErrorPage';
      }
    }else {
      location.href = '/';
    }
  }
}
